home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue52 / HTML / Code / AppServer / mleDataBindings.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-10-24  |  2.6 KB  |  107 lines

  1. unit mleDataBindings;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, mleCommon, mlePropertyInfo;
  7.  
  8. type
  9.   TDataBindingEntry = class(TCollectionItem)
  10.   public
  11.     DOClassName: string;  { data object class name }
  12.     OID: string;          { OID of the object instance }
  13.     InstanceName: string; { Name of the object instance }
  14.     PropertyName: string; { Name of the object property }
  15.     ControlName: string;  { HTML control name }
  16.   end;
  17.  
  18.   TDataBindings = class(TCollection)
  19.   protected
  20.     function GetItem(aIndex: Integer): TDataBindingEntry;
  21.   public
  22.     constructor Create;
  23.     procedure Add(aPropertyInfo: TPropertyInfo; aControlName: string);
  24.     function GetAsXML: string;
  25.     property Items[aIndex: Integer]: TDataBindingEntry read GetItem; default;
  26.   end;
  27.  
  28. implementation
  29.  
  30. uses
  31.   SysUtils;
  32.  
  33. { TDataBindings }
  34.  
  35. procedure TDataBindings.Add(aPropertyInfo: TPropertyInfo;
  36.   aControlName: string);
  37. begin
  38.   with TDataBindingEntry(inherited Add) do
  39.   begin
  40.     DOClassName := aPropertyInfo.Instance.ClassName;
  41.     OID := aPropertyInfo.Instance.OID;
  42.     InstanceName := aPropertyInfo.Instance.Name;
  43.     PropertyName := aPropertyInfo.PropertyName;
  44.     ControlName := aControlName;
  45.   end;
  46. end;
  47.  
  48. constructor TDataBindings.Create;
  49. begin
  50.   inherited Create(TDataBindingEntry);
  51. end;
  52.  
  53. function TDataBindings.GetAsXML: string;
  54. var
  55.   I: Integer;
  56.   XML: TStringList;
  57.   AClassName: string;
  58.   AOID: string;
  59. begin
  60.   Result := '';
  61.   if Count = 0 then Exit;
  62.   XML := TStringList.Create;
  63.   try
  64.     XML.Add('<databindings>');
  65.     AClassName := '';
  66.     AOID := '';
  67.     for I := 0 to Count - 1 do
  68.     begin
  69.       with Items[I] do
  70.       begin
  71.         if (OID <> AOID) or (DOClassName <> AClassName) then
  72.         begin
  73.           if XML.Count <> 1 then
  74.           begin
  75.             XML.Add('    </bindings>');
  76.             XML.Add('  </instance>');
  77.           end;
  78.  
  79.           AOID := OID;
  80.           AClassName := DOClassName;
  81.           XML.Add(Format('  <instance class="%s" oid="%s" name="%s">', [AClassName, AOID, InstanceName]));
  82.           XML.Add('    <bindings>');
  83.         end;
  84.  
  85.         XML.Add('      <binding>');
  86.         XML.Add('        <control>' + ControlName + '</control>');
  87.         XML.Add('        <property>' + PropertyName + '</property>');
  88.         XML.Add('      </binding>');
  89.       end;
  90.     end;
  91.  
  92.     XML.Add('    </bindings>');
  93.     XML.Add('  </instance>');
  94.     XML.Add('</databindings>');
  95.     Result := XML.Text;
  96.   finally
  97.     XML.Free;
  98.   end;
  99. end;
  100.  
  101. function TDataBindings.GetItem(aIndex: Integer): TDataBindingEntry;
  102. begin
  103.   Result := TDataBindingEntry(inherited Items[aIndex]);
  104. end;
  105.  
  106. end.
  107.